<?php

namespace App\Console\Commands\Kms;

use App\Models\Kms\Album\AlbumResourceModel;
use Illuminate\Console\Command;

class InitKmsAlbumResourceJoinRecycleAt extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'kms:InitAlbumResourceJoinRecycleAt';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化运营app相册加入回收站时间';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        AlbumResourceModel::query()
            ->where("isInRecycleBin", AlbumResourceModel::IS_IN_RECYCLE_BIN_YES)
            ->where("joinRecycleAt", 0)
            ->where("isDeleted", 0)
            ->chunkById(
                100, function ($item) {
                foreach ($item as $v) {
                    AlbumResourceModel::query()->where("id", $v['id'])
                        ->update(
                            [
                                'joinRecycleAt' => $v['updatedAt'],
                            ]
                        );

                    $this->info(
                        vsprintf(
                            "【id : %s】 - 【加入回收站时间 : %s】",
                            [
                                $v['id'],
                                formatDate($v['updatedAt']),
                            ]
                        )
                    );
                }
            }
            );
    }
}
